<template>
  <view class="container">
    <div class="d-article">
			<div class="d-article-head">
				<h2 class="d-article-caption">{{ note.title }}</h2>
			</div>
			<div class="d-article-content active">
				<div>{{ note.content }}</div>
        <uni-file-picker
          :value="imageList"
          :limit="1000"
          title=""
          file-mediatype="image"
          :readonly="true"></uni-file-picker>
			</div>
			<div class="d-article-attr">
				<span class="d-item-attr">创建：{{ simpleFormat(note.createTime) }}</span>
				<span v-if="note.updateTime" class="d-item-attr">更新：{{ simpleFormat(note.updateTime) }}</span>
			</div>
		</div>
  </view>
</template>

<script>
  import noteSer from '@/api/service/note.js'
  import fileSer from '@/api/service/file.js'
	import { parseTime } from '@/utils/ruoyi.js'
  export default {
    data() {
      return {
				params: {},
        note: {
          title: "",
          content: "",
					createTime: '',
					updateTime: ''
        },
        imageList: []
      }
    },
    onLoad(params) {
    	this.params = params
    },
		onShow(params) {
			this.initView()
    },
    onReady() {

    },
		onNavigationBarButtonTap (item) {
			// 删除
			if (item.index === 0) {
				this.handleDelete()
			}
			// 编辑
			else {
				this.handleEdit()
			}
		},
    methods: {
			async initView () {
				let params = this.params || {}
				let node = await noteSer.queryItem({ id: params.id || -9999 })
				if (node) {
					this.isEdit = true
					this.note = node
          let images = node.images
          if (images) {
            fileSer.queryList({ id: images }, { operators: [{field: 'id', operator: 'IN'}] }).then(res => {
              let list = Array.isArray(res) ? res : []
              this.imageList = list.map(item => {
                return {
                  id: item.id,
                  name: item.name,
                  url: item.path
                }
              })
            })
          }
				}
			},
			handleEdit () {
				this.$tab.navigateTo('/pages/note/edit?id=' + this.note.id)
			},
			handleDelete () {
				uni.showModal({
					title: '提示',
					content: '确认删除该条记录吗？',
					success: (res) => {
						if (res.confirm) {
							let defer = noteSer.delete({ id: this.note.id, images: this.note.images })
							defer.then(() => {
								this.$u.toast('操作成功')
								uni.navigateBack({
									url: '/pages/note/index'
								})
							}).catch(() => {
								this.$u.toast('操作失败')
							})
						}
					}
				})
			},
			simpleFormat(time) {
				return parseTime(time)
			}
    }
  }
</script>

<style lang="scss" scoped>
.container {
	min-height: 100vh;
	background: #ffffff;
	.d-article {
		.d-article-content {
			min-height: 82vh;
		}
		.d-article-attr {
			display:block;
			padding: 8px 0 0;
			text-align: left;
			border-top: 1px dashed #e0e0e0;
			.d-item-attr {
				display:block;
				margin-left: 0;
				margin-right: 0;
			}
		}
	}
}
</style>
